# Mappings
from sqlalchemy import orm

from cibynet.model import meta
from cibynet.model import tables as t
from cibynet.model.all_objects import *


orm.mapper(Building, t.buildings,
           properties = {
   'rooms': orm.relation(Room, backref="building"),
   'label': orm.synonym("_label", map_column=True),
                         })

orm.mapper(Ip, t.ips,
           properties = {
  'machinesassociation': orm.relation(MachineIp, cascade="all",
                                      order_by=t.machines_ips.c.from_date.desc()),
                         })

orm.mapper(Machine, t.machines,
           properties = {
  'ipassociations': orm.relation(MachineIp, cascade="all",
                                 order_by=t.machines_ips.c.from_date.desc()),
                         })

orm.mapper(MachineIp, t.machines_ips, properties={
    'machine': orm.relation(Machine),
    '_ip': orm.relation(Ip),
    "to_date": orm.synonym("_to_date", map_column=True),
})

orm.mapper(User, t.users, properties={
  'machines': orm.relation(Machine, backref="user"),
  'feedbacks': orm.relation(Feedback, backref="user"),
  'firstname': orm.synonym("_firstname", map_column=True),
  'lastname': orm.synonym("_lastname", map_column=True),
  'username': orm.synonym("_username", map_column=True),
  'email': orm.synonym("_email", map_column=True),
  'password': orm.synonym("_password", map_column=True),
})

orm.mapper(Group, t.groups, properties={
  'name': orm.synonym("_name", map_column=True),
                                        })

orm.mapper(UserGroup, t.users_groups, properties={
  'user': orm.relation(User, lazy=True, 
                       backref=orm.backref('groupassociations')),
  'group': orm.relation(Group, lazy=True, 
                       backref=orm.backref('userassociations')),
                                                  })


orm.mapper(FailedLoginAttempt, t.failed_login_attempts)
orm.mapper(LoginTemporizer, t.login_temporizing)

orm.mapper(Room, t.rooms, properties={
  'switchport': orm.relation(SwitchPort, backref="room", uselist=False),
  'label': orm.synonym("_label", map_column=True),
})

orm.mapper(RoomUser, t.rooms_users, properties={
    'user': orm.relation(User, lazy=True, order_by=t.rooms_users.c.id.desc,
                         backref=orm.backref("roomassociations",
                                             order_by=t.rooms_users.c.from_date.desc()),
                         cascade="all"),
    'room': orm.relation(Room, lazy=True, order_by=t.rooms_users.c.id.desc,
                         backref=orm.backref("userassociations", 
                                             order_by=t.rooms_users.c.from_date.desc()),
                         cascade="all"),
})


orm.mapper(Switch, t.switchs,
           properties = {
   'ports': orm.relation(SwitchPort, backref="switch"),
   "ip": orm.synonym("_ip", map_column=False),
                         })

orm.mapper(SwitchPort, t.switchports)

orm.mapper(Feedback, t.feedbacks)


orm.mapper(Check, t.checks, properties={
  'issuer': orm.synonym("_issuer", map_column=True),
})


orm.mapper(Bank, t.banks, properties = {
    'checks': orm.relation(Check, backref="bank"),
    'name': orm.synonym("_name", map_column=True),
                                        })

orm.mapper(Registration, t.registrations, properties={
  'room': orm.relation(Room),
  'user': orm.relation(User, 
                       primaryjoin=t.registrations.c.user_id==t.users.c.id),
  'check': orm.relation(Check),
})

